package com.dlwx.parttimetalent.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.dlwx.parttimetalent.R;
import com.dlwx.parttimetalent.bean.BaseBean;
import com.dlwx.parttimetalent.bean.OrderBean;
import com.dlwx.parttimetalent.bean.UnReadBean;
import com.dlwx.parttimetalent.comm.Constant;
import com.dlwx.parttimetalent.comm.Http;
import com.dlwx.parttimetalent.inter.PositionInter;
import com.dlwx.parttimetalent.ui.ComplaintActivity;
import com.dlwx.parttimetalent.ui.EvaluateActivity;
import com.dlwx.parttimetalent.ui.MapActivity;
import com.dlwx.parttimetalent.utils.CallPhoneUtil;
import com.dlwx.parttimetalent.utils.DealResult;
import com.dlwx.parttimetalent.utils.OkgoUtil;
import com.dlwx.parttimetalent.utils.SPUtils;
import com.dlwx.parttimetalent.utils.StringUtil;
import com.dlwx.parttimetalent.utils.ToastUtils;
import com.dlwx.parttimetalent.views.MyProgressLoading;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

/**
 * Created by Administrator on 2017/11/21.
 */

public class OrderEmployeAdapter extends ListBaseAdapter<OrderBean.BodyBean> {
    private final MyProgressLoading loadingDialog;
    private  Context context;
    private int orderType;
    private PositionInter inter;
    public OrderEmployeAdapter(Context context,PositionInter inter) {
        super(context);
        this.context=context;
        this.inter=inter;
        loadingDialog=new MyProgressLoading(context,R.style.DialogStyle);
    }

    @Override
    public int getLayoutId() {
        return R.layout.item_order_employe;
    }

    @Override
    public void onBindItemHolder(SuperViewHolder holder, int position) {
        final OrderBean.BodyBean bean=getDataList().get(position);
       ImageView image_header= holder.getView(R.id.image_header);
       TextView order_type= holder.getView(R.id.order_type);
       TextView tv_status= holder.getView(R.id.tv_status);
       TextView tv_name= holder.getView(R.id.tv_name);
       TextView tv_phone= holder.getView(R.id.tv_phone);
       TextView tv_time= holder.getView(R.id.tv_time);
       TextView tv_money= holder.getView(R.id.tv_money);
       TextView tv_location= holder.getView(R.id.tv_location);
       LinearLayout ll_location= holder.getView(R.id.ll_location);
//
        if(TextUtils.equals(bean.getStatusText(),"已接受")){
            ll_location.setVisibility(View.VISIBLE);
        }else{
            ll_location.setVisibility(View.INVISIBLE);

        }
       TextView tvDelete= holder.getView(R.id.tv_delete);
       TextView tvCancel= holder.getView(R.id.tv_cancel);
       TextView tvComment= holder.getView(R.id.tv_comment);
       TextView tvExplain= holder.getView(R.id.tv_explain);

//        tv_name.setText("下单人："+bean.getNickname());
        String remarkName=bean.getRemarkName();
        if(TextUtils.isEmpty(remarkName)){
            tv_name.setText("下单人:"+ StringUtil.subNameShort(bean.getNickname()));
        }else{
            tv_name.setText("下单人:"+StringUtil.subNameShort(remarkName));
        }
        Glide.with(context).load(bean.getHeadurl()).into(image_header);
        order_type.setText("我的接单:"+bean.getLabelname());
        tv_status.setText(bean.getStatusText());
        tv_money.setText("合计："+context.getString(R.string.rmb)+bean.getOrderTotal());

        tv_phone.setText(bean.getPhone_no());
        tv_phone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CallPhoneUtil.call(context,bean.getPhone_no());
            }
        });
        if(TextUtils.isEmpty(bean.getStarttime())){
            tv_time.setText("下单时间："+bean.getStartdate()+"~"+bean.getEnddate());
        }else {
            tv_time.setText("下单时间："+bean.getStartdate()+"~"+bean.getEnddate()+"     "+bean.getStarttime()+"~"+bean.getEndtime());
        }

        tv_location.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Double lat=null,lon = null;
                try {
                    if(!TextUtils.isEmpty(bean.getLat())){
                        lat= Double.parseDouble(bean.getLat());
                    };
                    if(!TextUtils.isEmpty(bean.getLongX())){
                        lon= Double.parseDouble(bean.getLongX());
                    };
                }catch (Exception e){
                    e.printStackTrace();
                }
                Intent i=new Intent(context, MapActivity.class);
                i.putExtra("lon",lon);
                i.putExtra("lat",lat);
                context.startActivity(i);

            }
        });
        final OrderBean.BodyBean.ButtonBean buttonBean = bean.getButton();
        if(TextUtils.isEmpty(buttonBean.getBtn1())){
            tvCancel.setVisibility(View.GONE);
        }else{
            tvCancel.setVisibility(View.VISIBLE);
            tvCancel.setText(buttonBean.getBtn1());
        }
        if(TextUtils.isEmpty(buttonBean.getBtn2())){
            tvComment.setVisibility(View.GONE);
        }else{
            tvComment.setVisibility(View.VISIBLE);
            tvComment.setText(buttonBean.getBtn2());
        }
        if(TextUtils.isEmpty(buttonBean.getBtn3())){
            tvExplain.setVisibility(View.GONE);
        }else{
            tvExplain.setVisibility(View.VISIBLE);
            tvExplain.setText(buttonBean.getBtn3());
        }

        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dealSome(buttonBean.getBtn1(),bean);
            }
        });
        tvComment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dealSome(buttonBean.getBtn2(),bean);
            }
        });
        tvExplain.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dealSome(buttonBean.getBtn3(),bean);
            }
        });
    }

    private void dealSome(String btnName,OrderBean.BodyBean bean) {
        Intent i=new Intent(context, ComplaintActivity.class);
        switch (btnName.trim()){
            case "删除订单":
                deleteOrder(bean.getOrderid());
                break;
            case "取消订单":
                cancleOrder(bean.getOrderid());
//                context.startActivity(new Intent(context, CancelOrderActivity.class));
                break;
            case "接受订单":
                acceptOrder(bean.getOrderid());
                break;
            case "拒绝订单":
                i.putExtra("id",bean.getOrderid()+"");
                i.putExtra("name","拒绝订单");
                i.putExtra("status",1+"");
                context.startActivity(i);
                break;
            case "评价":
                Intent intent=new Intent(context, EvaluateActivity.class);
                intent.putExtra("id",bean.getOrderid()+"");
                intent.putExtra("type",2+"");
                context.startActivity(intent);
                break;
            case "投诉":
                i.putExtra("id",bean.getOrderid()+"");
                i.putExtra("name","投诉");
                i.putExtra("status",2+"");
                context.startActivity(i);
                break;
        }
    }

    private void acceptOrder(int orderid) {
        loadingDialog.show();
        OkgoUtil.post(Http.AcceptOrder,context)
                .params("orderid",orderid)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        loadingDialog.dismiss();
                        BaseBean parentBean= DealResult.getInstance().dealDatas(context,response,new TypeToken<BaseBean>() {}.getType());
                        if(parentBean==null){return;}
                        int code=parentBean.getCode();
                        if(code== Constant.SUCESS){
                            inter.callBack(0);
                            getUnReadMsg();
                        }else {
                        }
                        ToastUtils.toastForShort(context,parentBean.getResult());
                    }
                    @Override
                    public void onError(Response<String> response) {
                        loadingDialog.dismiss();
                        super.onError(response);
                        ToastUtils.toastForShort(context,context.getString(R.string.bad_net));
                    }
                });
    }
    private void deleteOrder(int orderid) {
        loadingDialog.show();
        OkgoUtil.post(Http.DeleteOrder,context)
                .params("orderid",orderid)
                .params("orderType",2)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        loadingDialog.dismiss();
                        BaseBean parentBean= DealResult.getInstance().dealDatas(context,response,new TypeToken<BaseBean>() {}.getType());
                        if(parentBean==null){return;}
                        int code=parentBean.getCode();
                        if(code== Constant.SUCESS){
                            inter.callBack(0);
                        }else {
                        }
                        ToastUtils.toastForShort(context,parentBean.getResult());
                    }
                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        loadingDialog.dismiss();
                        ToastUtils.toastForShort(context,context.getString(R.string.bad_net));
                    }
                });
    }
    private void cancleOrder(int orderid) {
        loadingDialog.show();
        OkgoUtil.post(Http.CancelOrder,context)
                .params("orderid",orderid)
                .params("orderType",2)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        loadingDialog.dismiss();
                        BaseBean parentBean= DealResult.getInstance().dealDatas(context,response,new TypeToken<BaseBean>() {}.getType());
                        if(parentBean==null){return;}
                        int code=parentBean.getCode();
                        if(code== Constant.SUCESS){
                            inter.callBack(0);
                        }else {
                        }
                        ToastUtils.toastForShort(context,parentBean.getResult());
                    }
                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        loadingDialog.dismiss();
                        ToastUtils.toastForShort(context,context.getString(R.string.bad_net));
                    }
                });
    }

    private void getUnReadMsg() {
        OkgoUtil.post(Http.GetUserUnread, context)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        loadingDialog.dismiss();
                        UnReadBean parentBean = DealResult.getInstance().dealDatas(context
                                , response, new TypeToken<UnReadBean>() {
                                }.getType());
                        if (parentBean == null) {
                            return;
                        }
                        int code = parentBean.getCode();
                        if (code == Constant.SUCESS) {
                            UnReadBean.BodyBean bean = parentBean.getBody();
                            int orderNum = bean.getOrdernum();
                            int sysNum = bean.getSysnum();
                            int tradeNum = bean.getTradenum();
                            int gyNum = bean.getGynum();
                            int spNum = bean.getSpnum();
                            SPUtils.put(context, Constant.MSG_O, orderNum);
                            SPUtils.put(context, Constant.MSG_S, sysNum);
                            SPUtils.put(context, Constant.MSG_T, tradeNum);
                            SPUtils.put(context, Constant.MSG_SP, spNum);
                            SPUtils.put(context, Constant.MSG_GY, gyNum);
                        }
                    }
                    @Override
                    public void onError(Response<String> response) {
                        loadingDialog.dismiss();
                        super.onError(response);
                        ToastUtils.toastForShort(context, context.getString(R.string.bad_net));
                    }
                });
    }
}
